package net.Crystal.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import net.Crystal.database.ConnectionRe;
import net.Crystal.database.SQLquery;

public class LoginDao {
	private Connection conn;
	private ResultSet rs;
	private PreparedStatement ps;
	private ConnectionRe connections=new ConnectionRe();
	
	private static boolean DEBUG = true;
	
	public int Login(String user,String pass){
    	conn=connections.getNetSQL();
    	int re=0;
    	try {
			ps=conn.prepareStatement(SQLquery.login);
			ps.setString(1, user);
			ps.setString(2, pass);
			rs=ps.executeQuery();
			if (rs.next()) {
				if(DEBUG) System.out.println("username"+rs.getString("username"));
				if(DEBUG) System.out.println("userpass"+rs.getString("userpass"));
				re=1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return re;
    }
	
	public int Login(String user){
    	conn=connections.getNetSQL();
    	int re=0;
    	try {
			ps=conn.prepareStatement(SQLquery.login);
			ps.setString(1, user);
			ps.setString(2, "");
			rs=ps.executeQuery();
			if (rs.next()) {
				if(DEBUG) System.out.println("username"+rs.getString("username"));
				re=1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return re;
    }
}
